﻿using DroneAPI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace DroneExample
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private static bool isFlying = false;
        private static double sliderValue = 0;

        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        // returns the current state of the input controls
        public static InputState GetState()
        {
            return new InputState(0, 0, 0, sliderValue, isFlying);
        }

        // Event handler for the the connectButton click event
        private async void ConnectToDrone(object sender, RoutedEventArgs e)
        {
            try
            {
                await DroneConnection.ConnectToDrone();
                takeOffButton.IsEnabled = false;
                connectButton.Content = "Connecting";
            }
            catch
            {
                DroneConnection.CloseConnection();
                connectButton.Content = "Error";
            }

            takeOffButton.IsEnabled = true;

            if (DroneConnection.ConnectionExists())
            {
                connectButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                controlGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;

                Parallel.Invoke(() => DroneControl.ControlLoop());
            }
        }

        // Event handler for takeOffButton click event
        private void takeOff(object sender, RoutedEventArgs e)
        {
            if (!isFlying)
            {
                takeOffButton.Content = "Land";
                rotateSlider.Value = 0;
            }
            else
            {
                takeOffButton.Content = "Take off";
            }

            takeOffButton.IsEnabled = !rotateSlider.IsEnabled;
            isFlying = !isFlying;
        }

        // Event handler for rotateSlider valueChanged event
        private void sliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            sliderValue = rotateSlider.Value;
        }
    }
}
